﻿(function () {
    var appValid = angular.module('dateValidation', []);

    appValid.controller("dateValidator", function ($scope, $filter, Services) {
        $scope.datePattern = /^(?:(?:31(\/|-|\.|\s|,)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.|\s|,)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?(?:(?:19)[5-9]\d|(?:20)?[0-9]\d))$|^(?:29(\/|-|\.|\s|,)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.|\s|,)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?(?:(?:19)?[5-9]\d|(?:20)?[0-9]\d))$/;
        $scope.date = { ToChecked: "", FromChecked: "" };

        $scope.dateTimePattern = /^(?:(?:31(\/|-|\.|\s|,)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.|\s|,)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?(?:(?:19)[5-9]\d|(?:20)?[0-9]\d))\s(?:[01]?[0-9]|2[0-3]):?[0-5]?[0-9]$|^(?:29(\/|-|\.|\s|,)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))\s(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9]$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.|\s|,)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?(?:(?:19)[5-9]\d|(?:20)?[0-9]\d))\s(?:[01]?[0-9]|2[0-3]):?[0-5]?[0-9]$/;

        $scope.formatDate = "dd.MM.yyyy";
        $scope.formatDateTime = "dd.MM.yyyy H:mm";
        $scope.formatsDate = ["dd/MM/yyyy", "dd.MM.yyyy", "dd-MM-yyyy", "dd MM yyyy", "dd,MM,yyyy",
                              "dd/MM/yy", "dd.MM.yy", "dd-MM-yy", "dd MM yy", "dd,MM,yy"];
        $scope.formatsDateTime = ["dd/MM/yyyy H:mm", "dd.MM.yyyy H:mm", "dd-MM-yyyy H:mm", "dd MM yyyy H:mm", "dd,MM,yyyy H:mm",
                                  "dd/MM/yy H:mm", "dd.MM.yy H:mm", "dd-MM-yy H:mm", "dd MM yy H:mm", "dd,MM,yy H:mm",
                                  "dd/MM/yyyy Hmm", "dd.MM.yyyy Hmm", "dd-MM-yyyy Hmm", "dd MM yyyy Hmm", "dd,MM,yyyy Hmm",
                                  "dd/MM/yy Hmm", "dd.MM.yy Hmm", "dd-MM-yy Hmm", "dd MM yy Hmm", "dd,MM,yy Hmm"];
        $scope.addFormats = ["dd/MM", "dd.MM", "dd-MM", "dd MM", "dd,MM", "yyyy", "dd"];
        $scope.formatsDate = $scope.formatsDate.concat($scope.addFormats);

        $scope.reset = function (name, value, id){
            var objs = document.getElementsByName(name);
            var flag = false;
            for (var i = 0; i < objs.length; i++) {
                objs[i].value = $scope.getDateTimeString(value);
                flag = true;
            }
            if (flag)
                Services.boxCheck({ id: id });
        };

        $scope.resetDate = function (name, value, id) {
            var objs = document.getElementsByName(name);
            var flag = false;
            for (var i = 0; i < objs.length; i++){
                    objs[i].value = $scope.getDateString(value);
                    flag = true;
                }
            if (flag)
                Services.boxCheck({ id: id });
        };

        $scope.getDateString = function (date) {
            if (date === null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy');
        };

        $scope.getDateTimeString = function (date) {
            if (date === null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy H:mm');
        };

        $scope.check = function (date, checked) {
            if (checked)
                return kendo.parseDate(date, $scope.formatsDate);
            return '';
        };

        $scope.checkOld = function (date, checked, old) {
            if (checked)
                return kendo.parseDate(date, $scope.formatsDate);
            return old;
        };

        $scope.checkOldMatch = function (date, old) {
            if (date != null) {
                if (date.match($scope.dateTimePattern) !== null)
                    return kendo.parseDate(date, $scope.formatsDateTime);
                if (date.match($scope.datePattern) !== null)
                    return kendo.parseDate(date, $scope.formatsDate);
                return kendo.parseDate(date, $scope.addFormats);
            }
            return old;
        };

        $scope.toFixedAmount = function (value) {
            if (value == null || value == undefined)
                return '';
            if (value == 0)
                return '0.00';
            return value.toFixed(2);
        };
    });

    appValid.controller("orderController", function ($scope, $filter, Services, $timeout) {

        $scope.str = {
            VOD: '',
            LOD: '',
            DO: '',
            BOD: '',
            LoI: '',
            FID: ''
        };

        $scope.init = function (id) {
            $scope.id = id;
        };

        $scope.changeSelectionOrder = function (keyEvent) {
            if (keyEvent.which == 9) {
                $timeout(function () {
                    var obj = $('#InputVOD' + $scope.id);
                    obj.focus();
                    obj.select();
                });
                keyEvent.preventDefault();
            }
        };

        $scope.$on('changeOrderTab', function () {
            if ($scope.id == Services.changeOrder.Id) {
                switch (Services.changeOrder.name) {
                    case 'VOD':
                        $scope.str.VOD = Services.changeOrder.date;
                        break;
                    case 'LOD':
                        $scope.str.LOD = Services.changeOrder.date;
                        break;
                    case 'DO':
                        $scope.str.DO = Services.changeOrder.date;
                        break;
                    case 'BOD':
                        $scope.str.BOD = Services.changeOrder.date;
                        break;
                    case 'LoI':
                        $scope.str.LoI = Services.changeOrder.date;
                        break;
                    case 'FID':
                        $scope.str.FID = Services.changeOrder.date;
                        break;
                    default: break;
                }
            }
        });

    });
    
})()